package com.sphd.Restaurant.presenter;

import android.widget.Toast;

import com.android.volley.VolleyError;
import com.sphd.Restaurant.infos.CouponInfo;
import com.sphd.Restaurant.interfFace.VolleyCallBack;
import com.sphd.Restaurant.utils.JsonUtil;
import com.sphd.Restaurant.utils.VolleyErrorHelper;
import com.sphd.Restaurant.views.CoupinsDetailsView;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.List;

/**
 * Created by Hu on 2016/10/14.
 */

public class CoupinsDetailsP extends BasePresenter implements VolleyCallBack<JSONObject> {
    CoupinsDetailsView coupinsDetailsView;

    public CoupinsDetailsP(CoupinsDetailsView coupinsDetailsView) {
        this.coupinsDetailsView = coupinsDetailsView;
    }

    @Override
    public void addData(String url) {
        coupinsDetailsView.showdialog();
        sendJsonObjectUrl(url, this);
    }

    @Override
    public void onResponse(JSONObject object) {
        try {
            List<CouponInfo> couponInfoList = JsonUtil.getListFromString(object.getString("res"),CouponInfo.class);
            Date gtm = new Date(object.getString("currentTime"));
            coupinsDetailsView.sendRes(couponInfoList,gtm);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        coupinsDetailsView.dissmissdialog();
    }

    @Override
    public void onErrorResponse(VolleyError error) {
            coupinsDetailsView.dissmissdialog();
        Toast.makeText(context, VolleyErrorHelper.getMessage(error),Toast.LENGTH_SHORT).show();
    }
}
